<?php

function _tranxuantuan_upload() {
	$form_upload = drupal_get_form('_tranxuantuan_form_upload_form');
	return drupal_render($form_upload);
}

function _tranxuantuan_form_upload_form($form, &$form_state) {
 	$form['upload'] = array(
    '#type' => 'file',
    '#title' => t('Chọn file tải lên'),
    '#title_display' => 'invisible',
    '#size' => 22,
    '#theme_wrappers' => array(),
    '#weight' => -10,
	); 
	
	$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Tải lên'),			
  );
	return $form;
}
function _tranxuantuan_form_upload_form_validate($form, &$form_state) {
	$module_path = drupal_get_path('module', 'tranxuantuan');
	$file = file_save_upload('upload', array(
     // Validates file is really an image.
    'file_validate_extensions' => array('txt gif jpg jpeg'), // Validate extensions.
  ));
  // If the file passed validation:
  if ($file) {
    // Move the file, into the Drupal file system
    if ($file = file_move($file, 'public://kq')) {
      // Save the file for use in the submit handler.
      $form_state['storage']['upload'] = $file;
    }
    else {
      form_set_error('upload', t('Failed to write the uploaded file to the site\'s file folder.'));
    }
  }
  else {
    form_set_error('upload', t('No file was uploaded.'));
  }
}
function _tranxuantuan_form_upload_form_submit($form, &$form_state) {

  $file = $form_state['storage']['upload'];
  // We are done with the file, remove it from storage.
  unset($form_state['storage']['upload']);
  // Make the storage of the file permanent
  $file->status = FILE_STATUS_PERMANENT;
  // Save file status.
  file_save($file);
	//dpm($file);
  drupal_set_message(t('Tập tin đã được tải lên thành công, filename: @filename.', array('@filename' => $file->filename)));// Set a response to the user.
 	
}